package com.zg.common.core.dao.scf.mapper;

import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
import com.baomidou.mybatisplus.core.mapper.BaseMapper;
import com.baomidou.mybatisplus.core.toolkit.Constants;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.zg.common.core.dao.scf.entity.ImgCategoryScore;
import com.zg.common.core.dao.scf.entity.ImgConditionScore;
import com.zg.common.core.req.PageReq;
import org.apache.ibatis.annotations.Mapper;
import org.apache.ibatis.annotations.Param;
import org.apache.ibatis.annotations.Select;

@Mapper
public interface ImgCategoryScoreMapper extends BaseMapper<ImgCategoryScore> {

    @Select("<script>" +
            "SELECT " +
            "cmp.id AS company_id, " +
            "cmp.credit_code, " +
            "cmp.company_name, " +
            "pvt.basic, " +
            "pvt.operation, " +
            "pvt.serious, " +
            "pvt.legal, " +
            "pvt.knowledge, " +
            "pvt.supply, " +
            "pvt.trans, " +
            "pvt.update_time " +
            "FROM scf_company cmp " +
            "LEFT JOIN ( " +
            "SELECT * " +
            "FROM ( " +
            "SELECT " +
            "ics.target_company_id, " +
            "ic.title, " +
            "ics.score, " +
            "ics.update_time " +
            "FROM IMG_CATEGORY_SCORE ics " +
            "LEFT JOIN IMG_CATEGORY ic " +
            "ON ics.CATEGORY_ID = ic.id " +
            ") " +
            "PIVOT ( " +
            "MAX(score) " +
            "FOR title IN ( " +
            "'基础信息'   AS basic, " +
            "'经营信息'   AS operation, " +
            "'严重违法'   AS serious, " +
            "'法律风险'   AS legal, " +
            "'知识能力'   AS knowledge, " +
            "'供应链'     AS supply, " +
            "'交易结算'   AS trans " +
            ") " +
            ") " +
            ") pvt ON cmp.id = pvt.target_company_id"+
            "<where>" +
            "  cmp.is_del = 0 " +
            "   <if test=\"pageReq.companyName != null and pageReq.companyName!= ''\"> AND cmp.company_name LIKE '%' || #{pageReq.companyName} || '%'</if>" +
            "   <if test=\"pageReq.creditCode != null and pageReq.creditCode!= ''\"> AND cmp.credit_code = #{pageReq.creditCode} </if>" +
            "   <if test=\"pageReq.companyCode != null and pageReq.companyCode!= ''\"> AND cmp.company_code = #{pageReq.companyCode} </if>" +
            "</where>" +
            "</script>")
    Page<ImgCategoryScore> page(Page<ImgCategoryScore> page,
                                @Param("pageReq") PageReq pageReq);

}
